home *** CD-ROM | disk | FTP | other *** search
- package icontrols.MaskedEdit;
-
- import com.ms.wd.core.Component;
- import com.ms.wd.core.Event;
- import com.ms.wd.ui.Control;
- import com.ms.wd.ui.Edit;
- import com.ms.wd.ui.KeyEvent;
- import com.ms.wd.win32.Windows;
-
- public class MaskedEdit extends Edit {
- private static final int CUT = 0;
- private static final int DELETE = 1;
- private static final int BACK = 2;
- private boolean allowPrompt;
- private boolean autoTab;
- private FormatText formatText;
- private Mask mask;
- private int maxLength;
- private boolean promptIncluded;
- private ValidationErrorEventHandler m_validationErrorHandler;
- private transient boolean canUndo;
- private transient boolean focus;
- private transient char lastChar;
- private transient int lastCode;
- private transient Object tag;
- private transient int undoEnd;
- private transient int undoStart;
- private transient String undoText;
-
- public void setText(String text) {
- if (!this.focus && this.formatText.getFormatString().length() != 0) {
- try {
- super.setText(this.formatText.reformatText(text));
- } catch (IllegalArgumentException var3) {
- super.setText("");
- this.onValidationError(new ValidationErrorEvent(5, -1));
- }
- } else {
- super.setText(text);
- }
-
- this.canUndo = false;
- }
-
- public char getPasswordChar() {
- return (char)((Control)this).sendMessage(210, 0, 0);
- }
-
- public void setPasswordChar(char passwordChar) {
- ((Control)this).sendMessage(204, passwordChar, 0);
- }
-
- public void setAutoTab(boolean autoTab) {
- this.autoTab = autoTab;
- }
-
- private void undo() {
- if (this.canUndo) {
- int start = ((Edit)this).getSelectionStart();
- int end = ((Edit)this).getSelectionEnd();
- String tmp = this.getText();
- super.setText(this.undoText);
- ((Edit)this).select(this.undoStart, this.undoEnd);
- this.undoText = tmp;
- this.undoStart = start;
- this.undoEnd = end;
- }
- }
-
- public int getMaxLength() {
- return this.maxLength;
- }
-
- public String getFormat() {
- return this.formatText.getFormatString();
- }
-
- private boolean maskTextInsert(String newText, boolean ignoreError) {
- int start = ((Edit)this).getSelectionStart();
- if (start >= this.mask.length()) {
- if (!ignoreError) {
- this.onValidationError(new ValidationErrorEvent(3, -1));
- Windows.MessageBeep(0);
- }
-
- return false;
- } else {
- char[] text = this.getText().toCharArray();
-
- for(int i = 0; i < newText.length(); ++i) {
- start = this.mask.skipLiteral(start, 2);
- if (start == text.length) {
- break;
- }
-
- int dstChar = this.mask.skipLiteral(text.length - 1, 1);
- if (!this.mask.isequalPromptChar(text[dstChar])) {
- if (!ignoreError) {
- this.onValidationError(new ValidationErrorEvent(1, -1));
- Windows.MessageBeep(0);
- }
-
- return false;
- }
-
- if (!this.mask.isValidCharacter(start, newText.charAt(i), this.isAllowPrompt())) {
- if (!ignoreError) {
- this.onValidationError(new ValidationErrorEvent(2, start));
- Windows.MessageBeep(0);
- }
-
- return false;
- }
-
- while(true) {
- dstChar = this.mask.skipLiteral(dstChar, 1);
- if (dstChar <= start) {
- text[start] = this.mask.checkCase(start, newText.charAt(i));
- ++start;
- break;
- }
-
- int srcChar = this.mask.skipLiteral(dstChar - 1, 1);
- if (!this.mask.isValidCharacter(dstChar, text[srcChar], true)) {
- if (!ignoreError) {
- this.onValidationError(new ValidationErrorEvent(2, dstChar));
- Windows.MessageBeep(0);
- }
-
- return false;
- }
-
- text[dstChar] = this.mask.checkCase(dstChar, text[srcChar]);
- --dstChar;
- }
- }
-
- super.setText(new String(text));
- start = this.mask.skipLiteral(start, 2);
- ((Edit)this).select(start, start);
- return start == this.mask.length();
- }
- }
-
- private void paste() {
- }
-
- public void setFormat(String format, int formatType) {
- this.formatText.setFormat(format, formatType);
- this.canUndo = false;
- if (!this.focus) {
- this.setRawText(this.getRawText());
- }
-
- }
-
- public void setMaxLength(int maxLength) {
- this.maxLength = maxLength;
- this.canUndo = false;
- }
-
- private void maskTextCut(int state) {
- boolean modified = false;
- int start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
- int end = this.mask.skipLiteral(((Edit)this).getSelectionEnd(), 2);
- if (state == 0) {
- if (start == end) {
- return;
- }
- } else if (start == end) {
- if (state == 1) {
- start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
- if (start == this.mask.length()) {
- return;
- }
-
- end = this.mask.skipLiteral(start + 1, 2);
- } else {
- if (state != 2) {
- throw new Error("maskTextCut: Internal Error");
- }
-
- end = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 1);
- if (end == 0) {
- return;
- }
-
- start = this.mask.skipLiteral(end - 1, 1);
- }
- }
-
- char[] text = this.getText().toCharArray();
- int srcChar = 0;
- int dstChar = 0;
-
- while(true) {
- srcChar = this.mask.skipLiteral(srcChar, 2);
- dstChar = this.mask.skipLiteral(dstChar, 2);
- if (srcChar == text.length) {
- break;
- }
-
- if (srcChar < start || srcChar >= end) {
- if (!this.mask.isValidCharacter(dstChar, text[srcChar], true)) {
- break;
- }
-
- text[dstChar] = this.mask.checkCase(dstChar, text[srcChar]);
- ++dstChar;
- if (state == 0 || state == 2 && this.lastChar != '\b' || state == 1 && this.lastCode != 46) {
- modified = true;
- }
- }
-
- ++srcChar;
- }
-
- for(; dstChar < srcChar; ++dstChar) {
- if (this.mask.isMetaChar(dstChar)) {
- text[dstChar] = this.getPromptChar();
- }
- }
-
- if (modified) {
- this.undoSave();
- }
-
- super.setText(new String(text));
- start = this.mask.skipLiteral(start, 2);
- ((Edit)this).select(start, start);
- }
-
- private void cut() {
- this.copy();
- this.maskTextCut(0);
- }
-
- public boolean isPromptIncluded() {
- return this.promptIncluded;
- }
-
- private void undoSave() {
- this.undoStart = ((Edit)this).getSelectionStart();
- this.undoEnd = ((Edit)this).getSelectionEnd();
- this.undoText = this.getText();
- this.canUndo = true;
- }
-
- protected void onKeyDown(KeyEvent e) {
- if (((Component)this).isDesignMode()) {
- e.handled = true;
- } else {
- if (this.mask.isMaskSet()) {
- switch (e.getKeyCode()) {
- case 45:
- if (e.isControl()) {
- this.copy();
- } else if (e.isShift()) {
- this.paste();
- }
-
- this.lastCode = e.getKeyCode();
- e.handled = true;
- return;
- case 46:
- if (e.isShift()) {
- this.cut();
- } else {
- this.maskTextCut(1);
- }
-
- this.lastCode = e.getKeyCode();
- e.handled = true;
- return;
- }
- }
-
- this.lastCode = e.getKeyCode();
- super.onKeyDown(e);
- }
- }
-
- protected void onLostFocus(Event e) {
- if (!((Component)this).isDesignMode()) {
- this.canUndo = false;
- if (this.mask.isMaskSet()) {
- String s = this.getText();
-
- for(int i = 0; i < s.length(); ++i) {
- if (!this.mask.isValidCharacter(i, s.charAt(i), this.isAllowPrompt())) {
- this.onValidationError(new ValidationErrorEvent(4, i));
- break;
- }
- }
- }
-
- if (this.formatText.getFormatString().length() != 0 && this.getRawText().length() != 0) {
- this.undoSave();
-
- try {
- this.focus = false;
- super.setText(this.formatText.reformatText(this.getText()));
- } catch (IllegalArgumentException var4) {
- this.canUndo = false;
- this.onValidationError(new ValidationErrorEvent(5, -1));
- }
- }
-
- this.focus = false;
- }
-
- super.onLostFocus(e);
- }
-
- public boolean isEnabled() {
- return Windows.IsWindowEnabled(((Control)this).getHandle());
- }
-
- public String toString() {
- return "{allowPrompt=" + this.allowPrompt + ",autoTab=" + this.autoTab + ",formatText=" + this.formatText.toString() + ",mask=" + this.mask.toString() + ",maxLength=" + this.maxLength + ",promptIncluded=" + this.promptIncluded + "}";
- }
-
- public MaskedEdit() {
- this("");
- }
-
- public MaskedEdit(String text) {
- this.allowPrompt = false;
- this.autoTab = false;
- this.formatText = new FormatText();
- this.mask = new Mask();
- this.maxLength = 0;
- this.promptIncluded = false;
- this.canUndo = false;
- this.focus = false;
- this.lastChar = 0;
- this.lastCode = 0;
- this.tag = null;
- this.undoEnd = 0;
- this.undoStart = 0;
- this.undoText = new String("");
- this.setText(text);
- }
-
- public boolean isAutoTab() {
- return this.autoTab;
- }
-
- public Object getTag() {
- return this.tag;
- }
-
- public void setTag(Object tag) {
- this.tag = tag;
- }
-
- public boolean isAllowPrompt() {
- return this.allowPrompt;
- }
-
- public char getPromptChar() {
- return this.mask.getPromptChar();
- }
-
- public void setPromptChar(char promptChar) {
- this.mask.setPromptChar(promptChar);
- this.canUndo = false;
- }
-
- public void setPromptIncluded(boolean promptIncluded) {
- this.promptIncluded = promptIncluded;
- this.canUndo = false;
- }
-
- private void copy() {
- int start = this.mask.skipLiteral(((Edit)this).getSelectionStart(), 2);
- int end = this.mask.skipLiteral(((Edit)this).getSelectionEnd(), 2);
- if (start != end) {
- ;
- }
- }
-
- protected void onKeyPress(KeyEvent e) {
- if (((Component)this).isDesignMode()) {
- e.handled = true;
- } else if (this.mask.isMaskSet()) {
- switch (e.keyChar) {
- case '\u0003':
- this.copy();
- break;
- case '\b':
- if ((Control.getModifierKeys() & 262144) != 0) {
- this.undo();
- e.handled = true;
- return;
- }
-
- this.maskTextCut(2);
- break;
- case '\u0016':
- this.paste();
- break;
- case '\u0018':
- this.cut();
- break;
- default:
- this.maskTextCut(0);
- if (this.maskTextInsert((new Character(e.keyChar)).toString(), false) && this.isAutoTab()) {
- ((Control)this).getParent().selectNextControl(this, true, true, true, false);
- }
- }
-
- this.lastChar = e.keyChar;
- e.handled = true;
- } else {
- if (this.getMaxLength() > 0) {
- if (e.keyChar != '\b' && this.getText().length() == this.getMaxLength()) {
- Windows.MessageBeep(0);
- e.handled = true;
- return;
- }
-
- if (this.isAutoTab() && ((Edit)this).getSelectionStart() == this.getMaxLength() - 1) {
- ((Control)this).getParent().selectNextControl(this, true, true, true, false);
- }
- }
-
- this.lastChar = e.keyChar;
- super.onKeyPress(e);
- }
- }
-
- public String getRawText() {
- if (!this.mask.isMaskSet()) {
- return this.getText();
- } else {
- String result = "";
- String text = this.getText();
- int enterable = 0;
-
- for(int i = 0; i < this.mask.length() && i < text.length(); ++i) {
- if (this.mask.isMetaChar(i)) {
- ++enterable;
- if (this.promptIncluded || !this.mask.isequalPromptChar(text.charAt(i))) {
- result = result + text.substring(i, i + 1);
- }
- }
- }
-
- if (this.promptIncluded) {
- while(result.length() < enterable) {
- result = result + this.mask.getPromptChar();
- }
- }
-
- return result;
- }
- }
-
- public void setRawText(String rawText) {
- if (!this.focus && this.formatText.getFormatString().length() != 0) {
- try {
- super.setText(this.formatText.reformatText(rawText));
- } catch (IllegalArgumentException var3) {
- super.setText("");
- this.onValidationError(new ValidationErrorEvent(5, -1));
- }
- } else if (!this.mask.isMaskSet()) {
- super.setText(rawText);
- } else {
- super.setText(this.mask.getEmptyMask());
-
- for(int i = 0; i < rawText.length() && !this.maskTextInsert(rawText.substring(i, i + 1), true); ++i) {
- }
- }
-
- this.canUndo = false;
- }
-
- protected void onGotFocus(Event e) {
- if (!((Component)this).isDesignMode()) {
- this.focus = true;
- if (this.formatText.getFormatString().length() != 0) {
- this.undo();
- }
- }
-
- super.onGotFocus(e);
- }
-
- public String getMask() {
- return this.mask.getMask();
- }
-
- public void setMask(String mask) {
- this.mask = new Mask(mask, this.mask.getPromptChar());
- this.setMaxLength(this.mask.length());
- this.canUndo = false;
- if (((Component)this).isDesignMode()) {
- super.setText(this.mask.getEmptyMask());
- } else {
- this.setRawText("");
- }
-
- }
-
- public void setEnabled(boolean enabled) {
- Windows.EnableWindow(((Control)this).getHandle(), enabled);
- }
-
- public ValidationErrorEventHandler getOnValidationError() {
- return this.m_validationErrorHandler;
- }
-
- protected void onValidationError(ValidationErrorEvent event) {
- if (this.m_validationErrorHandler != null) {
- this.m_validationErrorHandler.invoke(this, event);
- }
-
- }
-
- public void setOnValidationError(ValidationErrorEventHandler handler) {
- this.m_validationErrorHandler = handler;
- }
-
- public String getText() {
- if (!((Component)this).isDesignMode() && this.formatText.getFormatString().length() != 0 && !this.focus) {
- return this.canUndo ? this.undoText : "";
- } else {
- return super.getText();
- }
- }
-
- public void setAllowPrompt(boolean allowPrompt) {
- this.allowPrompt = allowPrompt;
- this.canUndo = false;
- }
- }
-